/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				 All right reserved. 		  </p>
 * 
 * <p>项目名称 ： 东莞交通工程质量监督综合业务系统 	  </p>
 * <p>创建者   :	ce 
 * 
 * <p>描   述  :   for CodeAction    </p>
 * 
 * <p>最后修改 : $Id: CodeAction.java,v 1.2 2012/02/01 03:10:55 zwm Exp $ </p>
 * 
 * 
*/ 
package com.pengtu.web.app;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Code;
import com.pengtu.service.app.SystemManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;
import com.pengtu.web.listener.StartupListener;

/**
 * 码表管理管理Action.
 * 
 * 码表的CRUD 
 * 
 * @author ce
 */
@Namespace("/app")
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "code.action", type = "redirect") })
public class CodeAction extends BaseActionSupport<Code> {
	private static final long serialVersionUID = -4052047494894591406L;
	private SystemManager systemManager;

	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

	// -- 页面属性 --//
	private String id;
	private Code entity;
	private Page<Code> page = new Page<Code>(10);// 每页10条记录

	// -- ModelDriven 与 Preparable函数 --//
	public Code getModel() {
		return entity;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = systemManager.getCode(id);
		} else {
			entity = new Code();
		}
	}

	// -- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("category");
			page.setOrder(Page.DESC);
		}
		page = systemManager.getCodeList(page, queryFilter);
		String tagUrl = Struts2Utils.getRequest().getHeader("Referer");
		Struts2Utils.setSessionAttribute("CODELIST_URL", tagUrl);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {

		return INPUT;
	}

	@Override
	public String save() throws Exception {
		systemManager.saveCode(entity);
		addActionMessage("保存代码成功");
		StartupListener.setupCodeListContext(Struts2Utils.getServletContext());
		String tagUrl = (String) Struts2Utils.getSessionAttribute("CODELIST_URL");
		if(StringUtils.isNotEmpty(tagUrl)){
			Struts2Utils.sendRedirectUrl(tagUrl);
			return null;
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		systemManager.deleteCode(id);
		addActionMessage("删除代码成功");
		return RELOAD;
	}
	
	public String view() throws Exception {

		return VIEW;
	}
	/**
	 * 定义在view()前执行二次绑定.
	 */
	public void prepareView() throws Exception {
		prepareModel();
	}
	
	public String getId() {
		return id;
	}

	public Page<Code> getPage() {
		return page;
	}

	public void setPage(Page<Code> page) {
		this.page = page;
	}

	// -- 页面属性访问函数 --//

}